package com.example.demo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.example.demo.dao.SysUserDao;
import com.example.demo.dao.SysUserRoleDao;
import com.example.demo.dao.UserDao;
import com.example.demo.datasources.DataSourceNames;
import com.example.demo.datasources.annotation.DataSource;
import com.example.demo.domain.SysUser;
import com.example.demo.domain.SysUserRole;
import com.example.demo.domain.User;
import com.example.demo.service.IUserService;

@Service
public class UserService extends ServiceImpl<UserDao, User> implements IUserService {

	@Autowired
	SysUserDao sysUserDao;
	
	@Autowired
	SysUserRoleDao sysUserRoleDao;
	
    @Override
    @DataSource(name = DataSourceNames.USER)
    @Transactional(value = "firstDataSourceTransactionManager")
    public Page<User> getUserPage() {
        return selectPage(new Page<User>(1, 5), null);
    }
    
    @Transactional(value = "transactionManager",rollbackFor = { ArithmeticException.class })
    @DataSource(name = DataSourceNames.USER)
    public String addUser(int a)  throws ArithmeticException {
    	SysUser user=new SysUser();
    	user.setUserId(2l);
    	user.setUsername("测试");
    	sysUserDao.insert(user);
    	System.out.println("添加user成功");
        int b = 1 / a;
        SysUserRole sysUserRole=new SysUserRole();
        sysUserRole.setId(2l);
        sysUserRole.setRoleId(3l);
        sysUserRoleDao.insert(sysUserRole);
        System.out.println("添加userRole成功");
        return "success";
    }
    @Override
    public List<String> queryAllPerms(Long userId){
    	
    	return sysUserDao.queryAllMenuId(userId);
    	
    }

}
